import { LogManagementService } from './log-management.service';
import { Component, OnInit, ViewChild, AfterViewInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import * as moment from 'moment';
import { differenceInCalendarDays } from 'date-fns';
import { MessagesService } from 'app/shared/service';

declare const Print: any;

@Component({
    selector: 'itmp-log-management',
    templateUrl: './log-management.component.html',
    styleUrls: ['./log-management.component.css']
})
export class LogManagementComponent implements OnInit {
    selectedUser = null; // 选中的每行tr数据
    listOfData = []; // 列表数据的数组
    renderHeader = null; // 表头数据的数组
    searchData: any; // 搜索数据的对象
    printDom: any; // 打印节点
    totalEle = 0;
    totalTemplate = false;
    current = 1;
    queryStartTime = null;
    queryEndTime = null;
    queryId = null;
    queryName = null;
    queryOpe = null;
    queryMethod = null;
    dateRange = null;
    queryPar = null;
    // 分页数据的对象
    pagination = {
        totalEle: 25,
        currentPage: 1,
        currentSize: 10,
        pageSize: 10
    };
    // 右上角的打印，放大等功能
    toolbarList = [{ className: 'icon-dayinfuyin-' }];
    constructor(private http: HttpClient, private LogService: LogManagementService, private msg: MessagesService) {}

    ngOnInit() {
        this.renderHeader = [
            {
                name: 'log_Id',
                key: null,
                value: 'log_Id',
                isChecked: true
            },
            {
                name: 'userName',
                key: null,
                value: 'userName',
                isChecked: true
            },
            {
                name: 'operationType',
                key: null,
                value: 'operationType',
                isChecked: true
            },
            {
                name: 'methodName',
                key: null,
                value: 'methodName',
                isChecked: true
            },
            {
                name: 'opsTime',
                key: null,
                value: 'opsTime',
                isChecked: true
            },
            {
                name: 'parameter',
                key: null,
                value: 'parameter',
                isChecked: true
            }
        ];
        this.getData();
    }
    // 获取数据
    getData() {
        const queryData = {
            logCondition: {
                dateTime: {
                    endTime: this.queryEndTime,
                    startTime: this.queryStartTime
                },
                description: this.queryOpe,
                userName: this.queryName
            },
            pageNum: this.pagination.currentPage,
            pageSize: this.pagination.pageSize
        };
        this.LogService.getUserLog(queryData).subscribe(res => {
            if (res.code === '000000') {
                this.listOfData = res.data.content;
                this.pagination.currentPage = res.data.pageNum;
                this.pagination.currentSize = res.data.numberOfElements;
                this.pagination.totalEle = res.data.totolElements;
            } else {
                this.msg.error(res['msg']);
            }
        });
    }
    // 分页控件改变事件
    onPageChange(e) {
        this.pagination.currentPage = e;
        this.getData();
    }
    onSizeChange(e) {
        this.pagination.pageSize = e;
        this.getData();
    }
    // 点击tr取值
    click(i): void {
        this.listOfData.forEach((data, index) => {
            data.active = false;
            if (i === index) {
                // 点击每个tr给与样式active
                data.active = true;
                const obj = {
                    log_Id: data.log_Id,
                    userName: data.userName,
                    operationType: data.operationType,
                    methodName: data.methodName,
                    opsTime: data.opsTime,
                    parame: data.parameter
                };
                this.selectedUser = obj;
            }
        });
    }
    // 刷新
    refresh(): void {
        this.cancelActive();
        this.queryName = null;
        this.dateRange = null;
        this.queryOpe = null;
        this.pagination.currentPage = 1;
        this.pagination.pageSize = 10;
        this.getData();
    }
    // 取消选中
    cancelActive(): void {
        this.listOfData.forEach((data, index) => {
            data.active = false;
        });
    }
    onChange(result: Date): void {
        this.queryStartTime = result[0]
            ? moment(result[0])
                  .startOf('day')
                  .valueOf()
            : null;
        this.queryEndTime = result[1]
            ? moment(result[1])
                  .endOf('day')
                  .valueOf()
            : null;
        this.getData();
    }
    getTimeStamp() {
        const nowdate = new Date();
        nowdate.setMonth(nowdate.getMonth() - 3);
        const y = nowdate.getFullYear();
        const m = nowdate.getMonth() + 1;
        const d = nowdate.getDate();
        const formatwdate = y + '-' + m + '-' + d;
        return new Date(formatwdate).getTime();
    }
    filterSearch() {
        this.pagination.currentPage = 1;
        this.pagination.currentSize = 10;
        this.getData();
    }
    userNameSearch() {
        this.pagination.currentPage = 1;
        this.pagination.currentSize = 10;
        this.getData();
    }
    operationSearch() {
        this.pagination.currentPage = 1;
        this.pagination.currentSize = 10;
        this.getData();
    }
    disabledDate = (current: Date): boolean => {
        // Can not select days before today and today
        return differenceInCalendarDays(current, new Date()) > 0;
    };
}
